<?php
function newEvent($start, $end, $title, $description){
	$file = fopen("example.xml", "r");
	$contents = fread($file, filesize("example.xml"));
	$updatedContent = str_replace("</data>",
			"<event start='".$start."' end='".$end."' isDuration='true' title='".$title."'>".$description."</event></data>", $contents);
	fclose($file);


	$handle = fopen("example.xml", 'w');
	fwrite($handle, $updatedContent);
	fclose($handle);
}

function createTimes($string){
	/*Cuts the string*/
	$dates = explode(' to ', $string);
	$start = str_replace('When: ','', $dates[0]);
	$middle = explode(' CET ', $dates[1]);
	$end = str_replace('Where:', '', $middle[0]);
	
	/*Levelling factor + checking PM/AM*/
	if(strstr($start, 'pm')){
		$pmstart = true;
	}
	else{
		$pmstart = false;
	}
	if(strstr($end, 'pm')){
		$pmend = true;
	}
	else{
		$pmend = false;
	}
	$start = str_replace('pm', '%', $start);
	$start = str_replace('am', '%', $start);
	$end = str_replace('pm', '%', $end);
	$end = str_replace('am', '%', $end);
	
	/*Take out the number of $start*/
	$i = strpos($start, "%");
	$newArr = str_split($start);
	if($newArr[$i - 2] !== " "){
		if($pmstart){
			$hhour ="" . $newArr[$i - 1] . $newArr[$i - 2];
			$hourstart = $hhour + 12;
		}
		else
			$hourstart ="" . $newArr[$i - 1] . $newArr[$i - 2];
	}
	else{
		if($startpm){
			$hhour = $newArr[$i - 1];
			$hourstart = $hhour + 12;
		}
		else{
			$hourstart = $newArr[$i - 1];
		}
	}
	
	/*Take out the number of $end*/
	$i = strpos($end, "%");
	$newArr = str_split($end);
	if($newArr[$i - 2] !== " "){
		if($pmend){
			$hhour ="" . $newArr[$i - 1] . $newArr[$i - 2];
			$hourend = $hhour + 12;
		}
		else
			$hourend ="" . $newArr[$i - 1] . $newArr[$i - 2];
	}
	else{
		if($pmend){
			$hhour = $newArr[$i - 1];
			$hourend = $hhour + 12;
		}
		else{
			$hourend = $newArr[$i - 1];
		}
	}

	/*Replaces *number*LL with nothing*/
	$i = strpos($start, "%");
	$newArr = str_split($start);
	$newArr[$i+1] = '';
	$newArr[$i-1] = '';
	$newArr[$i] = '';
	if($newArr[$i - 2] == 1)
		$newArr[$i - 2] = '';   /*Check is its 11 or 12 am/pm */
	foreach($newArr as $char){
		$final .= $char;
	}
	
	/*Replaces *number*LL with nothing*/
	$i = strpos($end, "%");
	$newArr = str_split($end);
	$newArr[$i+1] = '';
	$newArr[$i-1] = '';
	$newArr[$i] = '';
	if($newArr[$i - 2] == 1)
		$newArr[$i - 2] = '';
	foreach($newArr as $char){
		$final2 .= $char;
	}
	
	/*Result put in an array and returned*/
	$result[0] = $final . $hourstart.":00:00 GMT";
	$result[1] = $final2 . $hourend.":00:00 GMT";
	
	/*final check*/
	if(strlen($result[1]) < 15){
		$result[1] = $final . $hourend.":00:00 GMT";
	}
	return $result;
}

function str_split($string,$string_length=1) {
        if(strlen($string)>$string_length || !$string_length) {
            do {
                $c = strlen($string);
                $parts[] = substr($string,0,$string_length);
                $string = substr($string,$string_length);
            } while($string !== false);
        } else {
            $parts = array($string);
        }
        return $parts;
}
?>